﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MarvellousWorks.PracticalPattern.FactoryMethod.GenericFactory
{
    public class BatchFactoryBase<TCollection,TItem>:FactoryBase<TCollection>
        where TCollection : ProductCollection, new()
        where TItem : IProduct,new()
    {
        protected int quantity = 0;
        public virtual int Quantity { set { quantity = value; } }

        public override TCollection Create()
        {
            if (quantity <= 0) throw new ArgumentException("quantity");
            TCollection collection = new TCollection();
            for (int i = 0; i < quantity; i++)
            {
                collection.Insert(new TItem());
            }
            return collection;
        }
    }

    // 生产批量产品的实体工厂
    public class BatchProductAFactory : BatchFactoryBase<ProductCollection, ProductA>
    {

    }

    public class BatchProductBFactory : BatchFactoryBase<ProductCollection, ProductB>
    {

    }
}
